<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包实现命令模式</title>
</head>
<body>
    <button id="execute">打开</button>
    <button id="undo">关闭</button>
    <script>
        var Tv = {
            open:function () {
                console.log('打开电视机');
            },
            close:function () {
                console.log('关闭电视机');
            }
        };
        
        var createCommand = function (receiver) {
            var execute = function () {
                return receiver.open()
            };
            var undo = function () {
                return receiver.close();
            };
            return {
                execute:execute,
                undo:undo
            };
        };

        var setCommand = function (command) {
            document.getElementById('execute').onclick = function () {
                command.execute();
            }
            document.getElementById('undo').onclick = function () {
                command.undo();
            }
        };

        setCommand(createCommand(Tv));
    </script>
</body>
</html>